﻿using OsPortal;
using System;
using System.Data;
using System.Data.OleDb;
using System.IO;
using System.Text;
using System.Windows.Forms;

public class clsFileIO
{
    private static string strClassName = "clsFileIO";

    public static DataTable makeDataTableFromSheetName(string filename, string sheetName)
    {
        try
        {
            string connectionString = "";
            string str2 = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filename + ";Extended Properties='Excel 8.0;HDR=YES;';";
            string str3 = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filename + ";Extended Properties='Excel 12.0 XML;HDR=YES;';";
            connectionString = str2;
            int length = filename.Split(new char[] { '.' }).Length;
            string str4 = filename.Split(new char[] { '.' })[length - 1];
            if (str4.ToLower() == "xlsx")
            {
                connectionString = str3;
            }
            OleDbConnection connection = new OleDbConnection(connectionString);
            OleDbCommand selectCommand = new OleDbCommand("Select * From [" + sheetName + "$]", connection);
            connection.Open();
            OleDbDataAdapter adapter = new OleDbDataAdapter(selectCommand);
            DataTable dataTable = new DataTable();
            adapter.Fill(dataTable);
            return dataTable;
        }
        catch (Exception exception)
        {
            oFileHelper.WriteLogErr(strClassName, "makeDataTableFromSheetName", exception.ToString());
            return null;
        }
    }

    public static string readHtmlFile(string filename)
    {
        StringBuilder builder = new StringBuilder("");
        using (StreamReader reader = new StreamReader(filename))
        {
            string str = string.Empty;
            while ((str = reader.ReadLine()) != null)
            {
                builder.Append(str);
            }
        }
        return builder.ToString();
    }

    public static DataTable GetDataTableFromCsv(string path)
    {
        DataTable dataTable = new DataTable();
        String[] csv = File.ReadAllLines(path);

        int i = 0;
        foreach (string csvrow in csv)
        {
            var fields = csvrow.Split(','); // csv delimiter
            if (i==0)
            {
                dataTable.Columns.Add("Email");
                dataTable.Columns.Add("Name");
                i++;
            }
            var row = dataTable.NewRow();
            if (fields[0].Contains("@facebook.com"))
            {
                row["Email"] = fields[0];
                row["Name"] = fields[0].Split('@')[0];
                dataTable.Rows.Add(row); 
            }
        }

        return dataTable;
    }

    public static void ToCsV(DataGridView dGV, string filename)
    {
        string s = "";
        string str2 = "";
        int num = 0;
        while (num < dGV.Columns.Count)
        {
            str2 = str2.ToString() + Convert.ToString(dGV.Columns[num].HeaderText) + "\t";
            num++;
        }
        s = s + str2 + "\r\n";
        for (int i = 0; i < (dGV.RowCount - 1); i++)
        {
            string str3 = "";
            for (num = 0; num < dGV.Rows[i].Cells.Count; num++)
            {
                str3 = str3.ToString() + Convert.ToString(dGV.Rows[i].Cells[num].Value) + "\t";
            }
            s = s + str3 + "\r\n";
        }
        byte[] bytes = Encoding.GetEncoding(0x4e6).GetBytes(s);
        FileStream output = new FileStream(filename, FileMode.Create);
        BinaryWriter writer = new BinaryWriter(output);
        writer.Write(bytes, 0, bytes.Length);
        writer.Flush();
        writer.Close();
        output.Close();
    }
}

